"use strict";

var mysqlhelper=require( global.__BASE_PATH+'/lib/mysql-helper.js');
var default_user="nofeng";

exports.index = function(req, res, view, cache) {
	var user=(req.query.user!=null?req.query.user:default_user);
	return {user:user};
}

exports.load_index=function(req, res, view, cache) {
	var user=(req.query.user!=null?req.query.user:default_user);
	var sql="select title,img,content,date_format(article_date,'%Y-%m-%d')as article_date,style from article where user=? order by article_date desc";
	mysqlhelper.sql(sql,[user],function(rows){
		var data=[];
		for(var i=0;i<rows.length;i++){
			var row=rows[i];
			var title=row.title;
			var img=row.img;
			var content=row.content;
			var article_date=row.article_date;
			var style=row.style;
			data[i]={"title":title,"content":content,"article_date":article_date,"style":style,"img":img};
		}
		response_json(req,res,data)
	});
}

function response_json(req, res,data){
	res.writeHead(200, {'Content-Type': 'text/json;charset=utf-8' });  
	var str = JSON.stringify(data);
	res.write(str); 
	res.end();
}
